package com.user.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.achievement.entity.Course;
import com.achievement.entity.Score;
import com.achievement.entity.Subject;
import com.achievement.entity.dto.ScoreDTO;
import com.achievement.service.ICourseService;
import com.achievement.service.IScoreService;
import com.achievement.service.ISubjectService;
import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.user.entity.Teacher;
import com.user.entity.dto.TeacherDTO;
import com.user.service.ITeacherService;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private ISubjectService subjectService;
	@Autowired
	private ICourseService courseService;
	@Autowired
	private IScoreService scoreService;
	
	@RequestMapping("/login")
	public @ResponseBody AjaxResponse login(String num,String password,HttpSession session) {
		try {
			TeacherDTO teacher = teacherService.login(num, password);
			if(teacher==null)return new AjaxResponse(false, "用户名或密码错误!");
			else {
				session.setAttribute("teacher", teacher);
				return new AjaxResponse(true, "登陆成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "系统出错！");
		}
	}
	@RequestMapping("/logout")
	public void logout(HttpSession session,HttpServletResponse response) {
		try {
			session.removeAttribute("teacher");
			response.sendRedirect("/exam/tch_login");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	@RequestMapping("/getIndex")
	public void getIndex(HttpServletResponse response) throws IOException {
		response.sendRedirect("/exam/tch_index.jsp");
	}
	@RequestMapping("/findAll")
	public @ResponseBody List<Teacher> findAll(){
		try {
			return teacherService.findAll();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/getSubjects")
	public @ResponseBody Set<Subject> getSubjects(HttpSession session){
		try {
			TeacherDTO teacher = (TeacherDTO) session.getAttribute("teacher");
			if(teacher!=null) {
				return teacherService.getSubjects(teacher.getId());
			}
			List<Subject> list = subjectService.findAll();
			Set<Subject> result = new HashSet<Subject>();
			for(Subject s:list) {
				result.add(s);
			}
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<Teacher> findPage(JpaPageable pageable,String str,Long dept_id,Long subject_id){
		try {
			return teacherService.findPage(TeacherDTO.getSpecification(str,dept_id,subject_id), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(String teacher) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			Teacher obj = objectMapper.readValue(teacher,Teacher.class);
			if(teacherService.isNumExist(obj))
				return new AjaxResponse(false, "教工号已存在!");
			teacherService.saveOrUpdate(obj);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/activate")
	public @ResponseBody AjaxResponse activate(Long id) {
		try {
			teacherService.activate(id);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/revoked")
	public @ResponseBody AjaxResponse revoked(Long id) {
		try {
			if(teacherService.hasCourse(id))
				return new AjaxResponse(false, "教师已任教课程，不允许注销用户!");
			teacherService.revoked(id);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/findListBySubj")
	public @ResponseBody List<Teacher> findListBySubj(Long subjId){
		try {
			return teacherService.findSelectingBySubj(subjId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/getCourseList")
	public @ResponseBody List<Course> getCourseList(HttpSession session){
		try {
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				return teacherService.getCourses(dto.getId());
			return courseService.findAll();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findScorePage")
	public @ResponseBody TablePage<Score> findScorePage(HttpSession session,JpaPageable pageable,String str,Long course_id){
		try {
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			List<Long> ids = new ArrayList<Long>();
			if(null!=dto) {
				ids = teacherService.getCoursesId(course_id);
			}
			return scoreService.findPage(ScoreDTO.getSpecification(ids, str, null, course_id), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/updatePwd")
	public @ResponseBody AjaxResponse updatePwd(HttpSession session,String old_pwd,String new_pwd,String new_pwd2) {
		try {
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(dto!=null) {
				if(!teacherService.isPwdTrue(dto.getId(), old_pwd))
					return new AjaxResponse(false, "密码错误!");
				if(!new_pwd.equals(new_pwd2)) {
					return new AjaxResponse(false, "两次输入密码必须相同!");
				}else {
					teacherService.updatePwd(dto.getId(), new_pwd);
					return new AjaxResponse(true, "修改成功!");
			}}
			return new AjaxResponse(false, "操作失败!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败!");
		}
	}
}
